﻿using System;
using System.IO;
using System.Text;

namespace Lanting.Utilities
{
    public class LogHelper
    {
        /// <summary>
        /// 写日志时加锁
        /// </summary>
        private static object m_Lock = new object();

        #region 写日志
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="path">文件前缀</param>                    
        public static void WriteLog(string content, string path = "")
        {
            lock (m_Lock)
            {
                string file_name = "/" + path + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                string server_path = "Logs/";
                string wl_path = AppDomain.CurrentDomain.BaseDirectory + server_path;
                if (!Directory.Exists(wl_path))
                    Directory.CreateDirectory(wl_path); //如果没有该目录，则创建
                StreamWriter sw = new StreamWriter(wl_path + file_name, true, Encoding.UTF8);
                sw.WriteLine("-----" + DateTime.Now.ToString() + "-----");
                sw.WriteLine(content);
                sw.Close();
            }            
        }
        #endregion

    }
}
